/*
 * @(#)ChangeLegendNamePiePlot.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved. 
 * This software is the confidential and proprietary information of DCIVision Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.core;

import java.awt.Insets;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.PieDataset;
import org.jfree.io.SerialUtilities;

/**
 * ChangeLegendNamePiePlot.java 
 * this class is overlay  the realization of JFreeChart,solve the long name in 0.921 editions can't show normally
 * 
 * @author greatwall shao
 * @company DCIVision Limited
 * @creation date 08/04/2006
 */
public class ChangeLegendNamePiePlot extends PiePlot implements Cloneable, Serializable {

  private static final int LABLEMAXLENGTH = 10;
  
  protected Log log = LogFactory.getLog(this.getClass().getName());
  
  public ChangeLegendNamePiePlot() {
    super();
    this.setLabelGenerator(new ChangeNamePieItemLabelGenerator());
    this.setInsets(new Insets(0, 5, 5, 5));
  }

  public ChangeLegendNamePiePlot(PieDataset dataset) {
    super(dataset);
    this.setLabelGenerator(new ChangeNamePieItemLabelGenerator());
    this.setInsets(new Insets(0, 5, 5, 5));
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.jfree.chart.plot.PiePlot#getLegendItems()
   */
  public LegendItemCollection getLegendItems() {
    LegendItemCollection lic = super.getLegendItems();
    LegendItemCollection lic2 = new LegendItemCollection();
    for (int i = 0; i < lic.getItemCount(); i++) {
      LegendItem li = lic.get(i);
      LegendItem li2 = new LegendItem(li.getLabel().length() > getLableLength() ? li.getLabel().substring(0, getLableLength()) + "...." : li.getLabel(), li.getLabel(), li.getShape(), li
          .isShapeFilled(), li.getPaint(), li.getStroke(), li.getOutlinePaint(), li.getOutlineStroke());
      lic2.add(li2);
      log.debug(li.getLabel());
    }
    return lic2;
  }

  private int getLableLength() {
    return LABLEMAXLENGTH;
  }

  /**
   * Returns a clone of the plot.
   * 
   * @return A clone.
   * @throws CloneNotSupportedException
   *           if some component of the plot does not support cloning.
   */
  public Object clone() throws CloneNotSupportedException {

    ChangeLegendNamePiePlot clone = (ChangeLegendNamePiePlot) super.clone();

    if (this.getDataset() != null) {
      this.getDataset().addChangeListener(clone);
    }

    return clone;

  }

  /**
   * Provides serialization support.
   * 
   * @param stream
   *          the output stream.
   * @throws IOException
   *           if there is an I/O error.
   */
  private void writeObject(ObjectOutputStream stream) throws IOException {
    stream.defaultWriteObject();
    SerialUtilities.writePaint(this.getSectionPaint(), stream);
    SerialUtilities.writePaint(this.getBaseSectionPaint(), stream);
    SerialUtilities.writePaint(this.getSectionOutlinePaint(), stream);
    SerialUtilities.writePaint(this.getBaseSectionOutlinePaint(), stream);
    SerialUtilities.writeStroke(this.getSectionOutlineStroke(), stream);
    SerialUtilities.writeStroke(this.getBaseSectionOutlineStroke(), stream);
    SerialUtilities.writePaint(this.getShadowPaint(), stream);
    SerialUtilities.writePaint(this.getLabelPaint(), stream);
    SerialUtilities.writePaint(this.getLabelBackgroundPaint(), stream);
    SerialUtilities.writePaint(this.getLabelOutlinePaint(), stream);
    SerialUtilities.writeStroke(this.getLabelOutlineStroke(), stream);
    SerialUtilities.writePaint(this.getLabelShadowPaint(), stream);
    SerialUtilities.writePaint(this.getLabelLinkPaint(), stream);
    SerialUtilities.writeStroke(this.getLabelLinkStroke(), stream);
  }

  /**
   * Provides serialization support.
   * 
   * @param stream
   *          the input stream.
   * @throws IOException
   *           if there is an I/O error.
   * @throws ClassNotFoundException
   *           if there is a classpath problem.
   */
  private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
    stream.defaultReadObject();
    this.setSectionPaint(SerialUtilities.readPaint(stream));
    this.setBaseSectionPaint(SerialUtilities.readPaint(stream));
    this.setSectionOutlinePaint(SerialUtilities.readPaint(stream));
    this.setBaseSectionOutlinePaint(SerialUtilities.readPaint(stream));
    this.setSectionOutlineStroke(SerialUtilities.readStroke(stream));
    this.setBaseSectionOutlineStroke(SerialUtilities.readStroke(stream));
    this.setShadowPaint(SerialUtilities.readPaint(stream));
    this.setLabelPaint(SerialUtilities.readPaint(stream));
    this.setLabelBackgroundPaint(SerialUtilities.readPaint(stream));
    this.setLabelOutlinePaint(SerialUtilities.readPaint(stream));
    this.setLabelOutlineStroke(SerialUtilities.readStroke(stream));
    this.setLabelShadowPaint(SerialUtilities.readPaint(stream));
    this.setLabelLinkPaint(SerialUtilities.readPaint(stream));
    this.setLabelLinkStroke(SerialUtilities.readStroke(stream));
  }
}
